import router from '@/router'
import { Indicator, MessageBox, Toast} from 'mint-ui'
import axios from 'libraries/axios'
import Platform from '@/supports/platform'

class oderHandle {
  // 再次购买
  handleBuy (products) {
    Platform.buyNow(products)
  }

  // 再次购买
  handleBuyAgain (products) {
    MessageBox.confirm('确定再次购买?', '提示').then(action => {
      Platform.buyNow(products)
    }).catch(err => {
      // 点击取消
      console.log(err)
    })
  }

  // 取消订单
  handleCancel (orderId) {
    MessageBox.confirm('是否取消订单?', '提示').then(action => {
      this.cancelOrder(orderId)
    }).catch(err => {
      // 点击取消
      console.log(err)
    })
  }

  async cancelOrder(orderId) {
    try {
      Indicator.open()

      const response = await axios.post('/platform/v1/order/cancel', {orderNo: orderId})
      if (response.status === 0) {
        Indicator.close()
        Toast('订单已取消')
      }
    } catch (e) {

    }
  }

  // 去支付
  handleGoPay (order) {
    Platform.goPay(order)
  }

  // 订单追踪
  handleFollowOrder (orderId) {
    router.push({name: 'orderFollow', params: {orderNo: orderId}})
  }

  // 确认收货
  handleMakeSure (orderId) {
    MessageBox.confirm('是否确认收货?', '提示').then(action => {
      this.cancelOrder(orderId)
    }).catch(err => {
      // 点击取消
      console.log(err)
    })
  }

  async sureOrder(orderId) {
    try {
      Indicator.open()

      const response = await axios.post('/platform/v1/order/confirm', {orderNo: orderId})
      if (response.status === 0) {
        Indicator.close()
        Toast('已确认收货')
      }
    } catch (e) {

    }
  }

  // 申请退款
  handleRefund () {

  }
}

export default new oderHandle()
